#include "CCLuaEngine.h"
#include "tolua++.h"
#include "tolua_fix.h"
#include "LuaCCTableView.h"

MyCCTableView* MyCCTableView::create(CCScrollViewDirection direct, CCSize size, CCArray *items){
  MyCCTableView *table = new MyCCTableView(items);
  table->initWithViewSize(size, NULL);
  table->autorelease();
  table->setDataSource(table);
  table->setDirection(direct);
  table->setDelegate(table);
  table->_updateContentSize();
  
  //table->setContentSize(size);
  
  //table->ignoreAnchorPointForPosition(false); 
  //table->setAnchorPoint(CCPointMake(0, 0));
  //CCPoint p = table->getAnchorPoint();
  //CCSize s = table->getContentSize();
  //CCLog("MyCCTableView::create %f, %f, %f, %f, %d", p.x, p.y, s.width, s.height, table->isIgnoreAnchorPointForPosition()); 
  //table->reloadData();
  return table;
}

MyCCTableView::MyCCTableView(CCArray *items){
  setItems(items);
}

MyCCTableView::~MyCCTableView(){
  for(int i=0; i<items_->count(); i++){
    items_->objectAtIndex(i)->release();
    cells_->objectAtIndex(i)->release();
  }
  items_->release();
}

void MyCCTableView::setItems(CCArray *items){
  items_ = items;
  items_->retain();
  cells_ = CCArray::create();
  cells_->retain();
  for(int i=0; i<items_->count(); i++){
    CCTableViewCell *cell = new CCTableViewCell();
    CCNode* node= (CCNode*)items_->objectAtIndex(i);
    node->setAnchorPoint(CCPointZero);
    cell->addChild(node);
    cells_->addObject(cell);
  }
}
CCTableViewCell* MyCCTableView::tableCellAtIndex(CCTableView *table, unsigned int idx){
  return (CCTableViewCell*)cells_->objectAtIndex(idx);
  //CCTableViewCell *cell = table->dequeueCell();
  //if (!cell) {
    CCTableViewCell *cell = new CCTableViewCell();
    cell->autorelease();
    CCNode* node= (CCNode*)items_->objectAtIndex(idx);
    node->setAnchorPoint(CCPointZero);
    //CCLog("MyCCTableView::tableCellAtIndex %d", idx);
    /*
    CCSize size = getContentSize(); 
    CCScrollViewDirection direction = getDirection();
    if(direction == kCCScrollViewDirectionHorizontal){
      node->setPosition();
    }else if(direction == kCCScrollViewDirectionVertical){
      node->setPosition();
    }*/
    cell->addChild(node);
  //}else{
  //}
  return cell;
}

unsigned int MyCCTableView::numberOfCellsInTableView(CCTableView *table){
  int count = items_->count();
  //CCLog("MyCCTableView::numberOfCellsInTableView %d", count);
  return count;
}

CCSize MyCCTableView::cellSizeForTable(CCTableView *table){
  CCNode* node = (CCNode*)items_->objectAtIndex(0);
  CCSize size = node->getContentSize();
  //CCLog("MyCCTableView::cellSizeForTable %f %f", size.width, size.height);
  return size;
}



int tolua_Cocos2d_CCTableView_create(lua_State* tolua_S){
  int direction = ((int)  tolua_tonumber(tolua_S,2,0));
  CCSize size = *((CCSize*)  tolua_tousertype(tolua_S,3,0));
  CCArray* items= ((CCArray*)  tolua_tousertype(tolua_S,4,0));
  
  
  CCLog("c_Cocos2d_CCTableView_create begin %f %f", size.width, size.height);
  MyCCTableView *tv = MyCCTableView::create((CCScrollViewDirection)direction, size, items);
  CCLog("c_Cocos2d_CCTableView_create end %f %f", size.width, size.height);
  MyCCTableView* tolua_ret = tv;
  int nID = (tolua_ret) ? (int)tolua_ret->m_uID : -1;
  int* pLuaID = (tolua_ret) ? &tolua_ret->m_nLuaID : NULL;
  toluafix_pushusertype_ccobject(tolua_S, nID, pLuaID, (void*)tolua_ret,"CCTableView");    
  CCLog("c_Cocos2d_CCTableView_create return %f %f", size.width, size.height);
  return 1;
}
